home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr13 / aurora2c.zip / CALCPAD.AML < prev    next >
Text File  |  1995-04-07  |  8KB  |  289 lines

  1.  
  2. /* ------------------------------------------------------------------ */
  3. /* Macro:        CALCPAD.AML                                          */
  4. /* Written by:   nuText Systems                                       */
  5. /*                                                                    */
  6. /* Description:  This macro displays a simple calculator pad with     */
  7. /*               basic arithmetic operations, a memory, and four      */
  8. /*               display lines. Floating point numbers are not        */
  9. /*               supported. The number on the bottommost display      */
  10. /*               line can be entered into the text of the current     */
  11. /*               edit window.                                         */
  12. /*                                                                    */
  13. /* Usage:        Select this macro from the 'Sample Utility Macros'   */
  14. /*               menu or from the macro picklist <shift f12>. Numbers */
  15. /*               and operations can be entered by using the keyboard  */
  16. /*               or the mouse.                                        */
  17. /*                                                                    */
  18. /*               If you use this macro regularly, it may be           */
  19. /*               convenient to assign it to a key. For example:       */
  20. /*                                                                    */
  21. /*                 object edit                                        */
  22. /*                    .                                               */
  23. /*                    .                                               */
  24. /*                   key <alt k>                                      */
  25. /*                     runmacro getbootpath + "MACRO\\CALCPAD.X"      */
  26. /*                   end                                              */
  27. /*                                                                    */
  28. /* ------------------------------------------------------------------ */
  29.  
  30.   include bootpath "define.aml"
  31.  
  32.   // define the colors to use
  33.   define
  34.     set calc_display_color         color darkgray  on green
  35.     set calc_display_curr_color    color black     on green
  36.     set calc_button_color          color black     on green
  37.     set calc_client_color          color black     on gray
  38.     set calc_border_color          color white     on gray
  39.     set calc_control_color         color yellow
  40.   end
  41.  
  42.   // display lines
  43.   var line1
  44.   var line2
  45.   var line3
  46.   var line4
  47.  
  48.   // display operators
  49.   var op0
  50.   var op1
  51.   var op2
  52.   var op3
  53.   var op4
  54.  
  55.   // numeric base
  56.   radix = 10
  57.  
  58.   // clear the calculator display
  59.   function cleardisp
  60.     line1 = ''
  61.     line2 = ''
  62.     line3 = ''
  63.     line4 = 0
  64.     op0 = ' '
  65.     op1 = ' '
  66.     op2 = ' '
  67.     op3 = ' '
  68.     op4 = ' '
  69.   end
  70.  
  71.   // scroll the display upwards
  72.   function scrollup
  73.     op0   = op1
  74.     line1 = line2
  75.     op1   = op2
  76.     line2 = line3
  77.     op2   = op3
  78.     line3 = line4
  79.     op3   = op4
  80.     line4 = 0
  81.     op4   = ' '
  82.   end
  83.  
  84.   // perform the operation currently on the display
  85.   function result
  86.     value = case op3
  87.               when '+'  line3 + line4
  88.               when '-'  line3 - line4
  89.               when '*'  line3 * line4
  90.               when '/'  line3 / line4
  91.               when 'm'  line3 mod line4
  92.               when '%'  (line3 * line4) / 100
  93.             end
  94.     scrollup
  95.     line4 = value
  96.   end
  97.  
  98.  
  99.   // create the calculator window
  100.   createwindow
  101.   setwinobj
  102.   setframe ">b"
  103.   setcolor  border_color    calc_border_color
  104.   setcolor  text_color      calc_client_color
  105.   setcolor  control_color   calc_control_color
  106.   settitle "Calculator"
  107.   setwinctrl '≡'
  108.   sizewindow 36 5 72 20 "ad"
  109.   setborder "1i"
  110.   setshadow 2 1
  111.  
  112.   // define strings to display in the keypad
  113.   keystr = "  C        %   mod   /    CE   7    8    9    *    MR   4    5    6    -    MS   1    2    3    +    M+   0       +/-   =  "
  114.  
  115.   x = 0
  116.   y = 0
  117.  
  118.   // draw the keypad
  119.   gotoxy 3 7
  120.   while y < 5 do
  121.     while x < 5 do
  122.       writestr keystr [y * 25 + (x * 5) + 1 : 5]  calc_button_color
  123.       writestr "  "
  124.       x = x + 1
  125.     end
  126.     x = 0
  127.     y = y + 1
  128.     if y < 5 then
  129.       writeline
  130.       writeline
  131.       gotoxy 3
  132.     end
  133.   end
  134.  
  135.   y = 0
  136.  
  137.   // clear the display
  138.   cleardisp
  139.  
  140.   // do for each keypress or buttonclick
  141.   loop
  142.  
  143.     // draw the calculator display
  144.     gotoxy 3 2
  145.     writestr op0 + (pad (thousands line1) 32 'r') calc_display_color
  146.     gotoxy 3 3
  147.     writestr op1 + (pad (thousands line2) 32 'r') calc_display_color
  148.     gotoxy 3 4
  149.     writestr op2 + (pad (thousands line3) 32 'r') calc_display_color
  150.     gotoxy 3 5
  151.     writestr (if? op3 == '=' ' ' op3) + (pad (thousands line4) 32 'r')
  152.              calc_display_curr_color
  153.  
  154.     // display the window and get the next key
  155.     keycode = getkey
  156.     chr = locase (char (keycode & 0ffh))
  157.  
  158.     case keycode
  159.  
  160.       // exit the calculator
  161.       when <esc>
  162.         break
  163.  
  164.       // mouse click
  165.       when <button>
  166.  
  167.         // left mouse button only
  168.         if button? 1 then
  169.  
  170.           case getregion
  171.  
  172.             // client area
  173.             // translate mouse click to keypress
  174.             when 1
  175.               buttonline = case virtorow
  176.                              when 7  "cb%m/"
  177.                              when 9  "e789*"
  178.                              when 11 "r456-"
  179.                              when 13 "s123+"
  180.                              when 15 "z0ti="
  181.                            end
  182.  
  183.               column = virtocol
  184.               buttonchar = buttonline [
  185.                              if     column >= 3  and column <= 7  then 1
  186.                              elseif column >= 10 and column <= 14 then 2
  187.                              elseif column >= 17 and column <= 21 then 3
  188.                              elseif column >= 24 and column <= 28 then 4
  189.                              elseif column >= 31 and column <= 35 then 5
  190.                              else 6
  191.                              end
  192.                            ]
  193.  
  194.               // simulate the appropriate keypress
  195.               if buttonchar then
  196.                 queuekey (getkeycode '<'+ buttonchar + '>')
  197.               end
  198.  
  199.             // close icon
  200.             when 51
  201.               break
  202.           end
  203.         end
  204.  
  205.       // clear the display
  206.       when <c>, <del>
  207.         cleardisp
  208.  
  209.       // clear the entry line
  210.       when <e>
  211.         line4 = 0
  212.         op4 = ' '
  213.         op3 = ' '
  214.  
  215.       // store
  216.       when <s>
  217.         memory = line4
  218.  
  219.       // recall
  220.       when <r>
  221.         line4 = memory
  222.  
  223.       // store + memory
  224.       when <z>
  225.         memory = memory + line4
  226.  
  227.       // change sign
  228.       when <i>
  229.         line4 = -line4
  230.         op4 = ' '
  231.         op3 = ' '
  232.  
  233.       // enter into text
  234.       when <t>, <ctrl enter>
  235.         queue "write" (thousands line4)
  236.         break
  237.  
  238.       // backspace
  239.       when <b>, <backspace>
  240.         line4 = line4 / radix
  241.         op4 = ' '
  242.         op3 = ' '
  243.  
  244.       // enter numeric characters
  245.       when <0>, <1>, <2>, <3>, <4>, <5>, <6>, <7>, <8>, <9>
  246.         if chr < radix then
  247.           if op3 == '=' then
  248.             scrollup
  249.           end
  250.           positive = line4 >= 0
  251.           oldline = line4
  252.           line4 = line4 * radix + chr
  253.           // test for overflow
  254.           if positive <> (line4 > 0) then
  255.             line4 = oldline
  256.           end
  257.         end
  258.  
  259.       // operators
  260.       when <+>, <->, <*>, </>, <%>, <m>
  261.         if op3 <> ' ' and op3 <> '=' then
  262.           op4 = '='
  263.           result
  264.         end
  265.         op4 = chr
  266.         scrollup
  267.  
  268.       // do the current operation
  269.       when <=>, <enter>
  270.         if op3 <> ' ' then
  271.           if op3 == '=' then
  272.             op4 = op2
  273.             scrollup
  274.             line4 = line2
  275.           end
  276.           op4 = '='
  277.           result
  278.         end
  279.  
  280.       // unrecognized key
  281.       otherwise
  282.         beep 200 70
  283.     end
  284.   end
  285.  
  286.   // destroy the calculator window
  287.   destroywindow
  288.  
  289.